<?php

namespace App\Http\Requests\Api;

class RoleRequest extends FormRequest
{
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules(): array
    {
        switch ($this->route()->getName()) {
            case 'roles.store':
                return [
                    'name' => 'required|min:3|max:60|unique:roles',
                    'label' => 'sometimes|max:20'
                ];

            case 'roles.update':
                $id = $this->segment(4); // 排除掉当前角色
                return [
                    'name' => 'sometimes|min:3|max:60|unique:roles,name,'.$id,
                    'label' => 'sometimes|max:20'
                ];

            case 'roles.menus':
            case 'roles.permissions':
                return [
                    'ids' => 'required|array',
                ];

            default:;
        }
    }

    public function messages(): array
    {
        return [
            'name.required' => '角色标识不能为空',
            'name.min' => '角色标识最小长度为3个字符',
            'name.max' => '角色标识最大长度为60个字符',
            'name.unique' => '角色标识已经存在',
            'label.max' => '角色名最大长度为20个字符',
        ];
    }
}
